<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-width="100px" class="login-form" >
      <div class="title-container">
        <h3 class="title">{{CompanyName}}</h3>
      </div>
     <div class="nav_top">
        <span @click="istype='login';loginForm.is_primary=1"  :class="['span_1',istype=='login'?'activ':'']">主账号登录</span>
        <span @click="istype='logins';loginForm.is_primary=2" :class="['span_2',istype=='logins'?'activ':'']">子账号登录</span>
      </div>
      <div v-if="istype=='login'" style="height: 200px;">
          <el-form-item label="账号" prop="parent_username">
            <el-input
              ref="username"
              v-model="loginForm.parent_username"
              placeholder="账号"
              type="text"
            />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
          <el-button  class="btn" :loading="loading" type="primary"  @click.native.prevent="handleLogin">登录</el-button>
      </div>
     <div v-else>
          <!-- <el-form-item label="主账号" prop="parent_username">
            <el-input
              ref="username"
              v-model="loginForm.parent_username"
              placeholder="账号"
              type="text"
            />
          </el-form-item> -->
          <el-form-item label="子账号" prop="child_username">
            <el-input
              v-model="loginForm.child_username"
              placeholder="账号"
              type="text"
            />
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
          <el-button  class="btn" :loading="loading" type="primary"  @click.native.prevent="handleLogin">登录</el-button>

      </div>
    </el-form>
  </div>
</template>

<script>
import { 
  getMenu
 } from '@/api/role/index'

 import {
   getCompanyName
  } from '@/api/user'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('账号不能为空'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    return {
      CompanyName:'',
      istype:'login',
      loginForm: {
        is_primary:'1',
        child_username:'',
        parent_username: '',
        password: ''
      },
      loginRules: {
        child_username:[{required: true, trigger: 'blur', validator: validateUsername }],
        parent_username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      Menulist:[]
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted(){
    this.get_CompanyName()
  },
  methods: {
    get_CompanyName(){
      getCompanyName().then(res=>{
        if(res.code == 200){
          this.CompanyName=res.data
          console.log(this.$store)
        }
      })
    },
    getgetMenu(){
      getMenu().then(res=>{
        if(res.code==200){
          res.data.forEach(item=>{
            
          })
        }
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push('/dashboard/dashboard')
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#313bad;
$cursor: #313bad;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-form-item__content{
    margin-left: 0!important;
    flex: 1;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    flex: 1;


    input {
      background: transparent;
      border: 1px solid #81a9d9;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    // border: 1px solid #7ea5d4;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    padding:5px 10px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    color: #454545;
  }
}

</style>

<style lang="scss" scoped>


$bg:#004700;
$dark_gray:#889aa4;
$light_gray:#6073dd;

.login-container {
  min-height: 100%;
  width: 100%;
  background:url(../../assets/login.jpg);
  overflow: hidden;
  position: relative;

  .login-form {
    position: relative;
    width: 520px;
    border-radius: 15px;
    background: #eee;
    max-width: 100%;
    padding:25px;
    margin: 0 auto;
    overflow: hidden;
    position: absolute;top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%);  transform: translate(-50%, -50%);

  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.nav_top{
  width: 200px;
  margin: 0 auto;
  margin-top: 25px;
  margin-bottom: 25px;
  border: 1px solid #6193ff;
  border-radius: 38px;
  display: flex;
  justify-content: space-between;
  span{
    display: inline-block;
    width: 100px;
    height: 38px;
    line-height: 38px;
    background: #6193ff;
    text-align: center;
    color: #fff;
    cursor: pointer;

  }
  span.span_1{
   border-top-left-radius: 50px;
   border-bottom-left-radius: 50px;
  }
  span.span_2{
    border-top-right-radius: 50px;
    border-bottom-right-radius: 50px;
  }
}

.activ{
  background: #fff !important;
  color: #6193ff!important;
  font-weight: bold;
}

.btn{
  width:100%;margin: 0 auto; height: 45px;line-height:0px;font-size: 16px; margin-bottom:30px;background-color:#6193ff
}

// ::v-deep input:-webkit-autofill{  
// -webkit-text-fill-color: #ededed !important;  
// // -webkit-box-shadow: 0 0 0px 1000px transparent  inset !important;  
// -webkit-box-shadow:none !important;  
// box-shadow:none !important;  
// background-color:transparent !important;  
// background-image: none !important;  
//  transition: background-color 50000s ease-in-out 0s !important; 
// }  
// input {  
//  background-color:transparent !important;  
// }

// ::v-deep input{
//   background:none!important;
//   box-shadow:none!important;
//   color: #fff;
// }

::v-deep input:-webkit-autofill{
	-webkit-text-fill-color: #ededed !important;
	-webkit-box-shadow: 0 0 0px 1000px transparent  inset !important;
  background-color:transparent!important;
  background-image: none!important;
  transition: background-color 50000s ease-in-out 0s!important; //背景色透明  生效时长  过渡效果  启用时延迟的时间
}

::v-deep input {
	 background-color:transparent;
}
</style>
